<?php

namespace app\admin\controller;

use app\admin\logic\AdminLogic;
use app\common\respond\HttpApi;

class AdminController
{
    /**
     * @Description 列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/07/09 16:56:50
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function list(HttpApi $respond)
    {
        $param = [
            'page' => (int)request()->get('page') ?: 1,
            'page_size' => (int)request()->get('page_size') ?: 10,
            'name' => request()->get('name'),
            'phone' => request()->get('phone'),
            'create_time_start' => request()->get('create_time_start'),
            'create_time_end' => request()->get('create_time_end')
        ];
        $rule = [
            'page' => 'require|integer|>:0',
            'page_size' => 'require|integer|>:0',
            'name' => 'max:20',
            'phone' => 'max:11',
            'create_time_start' => 'dateFormat:Y-m-d H:i:s',
            'create_time_end' => 'dateFormat:Y-m-d H:i:s',
        ];
        validate($rule)->check($param);

        $logic = new AdminLogic();
        $data = $logic->getList($param);

        return $respond->success($data);
    }

    /**
     * @Description 新增
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/07/09 18:19:44
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(HttpApi $respond)
    {
        $param = [
            'name' => request()->post('name'),
            'phone' => request()->post('phone')
        ];
        $rule = [
            'name' => 'require|max:20',
            'phone' => 'require|mobile',
        ];
        validate($rule)->check($param);

        $logic = new AdminLogic();
        $data = $logic->add($param['name'],$param['phone']);

        return $respond->success([]);
    }

    /**
     * @Description 编辑
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/07/09 18:24:57
     * @return \think\response\Json
     */
    public function edit(HttpApi $respond)
    {
        $param = [
            'id' => request()->post('id'),
            'name' => request()->post('name'),
            'phone' => request()->post('phone'),
            'position_id' => request()->post('position_id')
        ];
        $rule = [
            'id' => 'require|integer|>:0',
            'name' => 'max:20',
            'phone' => 'mobile',
            'position_id' => 'integer|>:0'
        ];
        validate($rule)->check($param);

        $logic = new AdminLogic();
        $data = $logic->edit($param['id'],$param);

        return $respond->success([]);
    }

    /**
     * @Description 删除
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/07/09 18:30:03
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del(HttpApi $respond)
    {
        $param = [
            'id' => request()->post('id'),
        ];
        $rule = [
            'id' => 'require|integer|>:0'
        ];
        validate($rule)->check($param);

        $logic = new AdminLogic();
        $data = $logic->del($param['id']);

        return $respond->success([]);
    }

    /**
     * @Description 详情
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/07/10 10:35:16
     * @return \think\response\Json
     */
    public function detail(HttpApi $respond)
    {
        $param = [
            'id' => request()->get('id'),
        ];
        $rule = [
            'id' => 'require|integer|>:0'
        ];
        validate($rule)->check($param);

        $logic = new AdminLogic();
        $data = $logic->detail($param['id']);

        return $respond->success($data);
    }

    /**
     * @Description 选择列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/07/10 10:35:26
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function selectList(HttpApi $respond)
    {
        $param = [
            'page' => (int)request()->get('page') ?: 1,
            'page_size' => (int)request()->get('page_size') ?: 10,
            'name' => request()->get('name'),
            'phone' => request()->get('phone')
        ];
        $rule = [
            'page' => 'require|integer|>:0',
            'page_size' => 'require|integer|>:0',
            'name' => 'max:20',
            'phone' => 'max:11'
        ];
        validate($rule)->check($param);

        $logic = new AdminLogic();
        $data = $logic->selectList($param);

        return $respond->success($data);
    }

    /**
     * @Description 修改密码
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/07/10 10:50:19
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function modifyPassword(HttpApi $respond)
    {
        $param = [
            'old_password' => request()->post('old_password'),
            'new_password' => request()->post('new_password'),
            'repassword' => request()->post('repassword')
        ];
        $rule = [
            'old_password' => 'require|length:6,20',
            'new_password' => 'require|length:6,20',
            'repassword' => 'require|confirm:new_password',
        ];
        validate($rule)->check($param);

        $logic = new AdminLogic();
        $data = $logic->modifyPassword($param['old_password'],$param['new_password']);

        return $respond->success([]);
    }

}